import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Req,
  UseGuards,
  ClassSerializerInterceptor,
  UseInterceptors,
  Query,
} from '@nestjs/common';
import { PublicityService } from './publicity.service';
import { CreatePublicityDto } from './dto/create-publicity.dto';
import { UpdatePublicityDto } from './dto/update-publicity.dto';
import { Request } from 'express';
import { JwtGuard } from '../guards/jwt.guards';
import { getCurrentDate } from '../utils';
import {
  ApiTags,
  ApiOperation,
  ApiBody,
  ApiResponse,
  ApiQuery,
} from '@nestjs/swagger';
import {
  GetPublicityDto,
  ReturnPublicityDtoList,
} from './dto/get-publicity.dto';

@Controller('publicity')
@UseInterceptors(ClassSerializerInterceptor)
@UseGuards(JwtGuard)
@ApiTags('学校公告')
export class PublicityController {
  constructor(private readonly publicityService: PublicityService) {}

  @Post()
  @ApiOperation({ summary: '创建公告' })
  @ApiBody({ type: CreatePublicityDto, description: '' })
  @ApiResponse({
    status: 200,
    description: '成功返回200，失败返回400',
    type: CreatePublicityDto,
  })
  create(@Body() createPublicityDto: CreatePublicityDto, @Req() req: Request) {
    createPublicityDto.created_name_id = req.user['sub'];
    createPublicityDto.created_name = req.user['username'];
    createPublicityDto.created_time = createPublicityDto.created_time
      ? createPublicityDto.created_time
      : getCurrentDate();
    return this.publicityService.create(createPublicityDto);
  }

  @Get()
  @ApiOperation({ summary: '公告列表' })
  @ApiQuery({ type: GetPublicityDto, description: '参数可选' })
  @ApiResponse({
    status: 200,
    description: '成功返回200，失败返回400',
    type: ReturnPublicityDtoList,
  })
  findAll(@Query() query: GetPublicityDto) {
    return this.publicityService.findAll(query);
  }

  @Get(':id')
  @ApiOperation({ summary: '公告详情' })
  @ApiQuery({ name: 'id', required: true, description: '公告id' })
  @ApiResponse({
    status: 200,
    description: '成功返回200，失败返回400',
    type: CreatePublicityDto,
  })
  findOne(@Param('id') id: number) {
    return this.publicityService.findOne(id);
  }

  @Patch(':id')
  @ApiOperation({ summary: '公告更新' })
  @ApiQuery({ name: 'id', required: true, description: '公告id' })
  @ApiBody({ type: CreatePublicityDto, description: '' })
  @ApiResponse({
    status: 200,
    description: '成功返回200，失败返回400',
    type: CreatePublicityDto,
  })
  update(
    @Param('id') id: number,
    @Body() updatePublicityDto: UpdatePublicityDto,
    @Req() req: Request,
  ) {
    updatePublicityDto.created_name_id = req.user['sub'];
    updatePublicityDto.created_name = req.user['username'];
    return this.publicityService.update(id, updatePublicityDto);
  }

  @Delete(':id')
  @ApiOperation({ summary: '公告删除' })
  @ApiQuery({ name: 'id', required: true, description: '公告id' })
  @ApiResponse({
    status: 200,
    description: '成功返回200，失败返回400',
    type: CreatePublicityDto,
  })
  remove(@Param('id') id: number) {
    return this.publicityService.remove(id);
  }
}
